<template>
	<view class="select-staff-main">
		<view class="search w-140 mr30 mb20">
			<text class="iconfont .icon-sousuo"></text>
			<input class="weui-input w480" confirm-type="search" @confirm="confirmHandle" placeholder="请输入人员名称"
				v-model="query.nickName" />
			<image @click="clearInputHandle" class="w36 h36 mr20"
				src="https://fireware-1257276602.cos.ap-guangzhou.myqcloud.com/fsw-wx-lmini/icons/delico.png"
				mode="aspectFit|aspectFill|widthFix" lazy-load="false" binderror="" bindload=""></image>
		</view>
		<scroll-view scroll-y="true" @scrolltolower="scrolltolowerHandle" class="staff-list">
			<view @click="checkItemHandle(item)" class="item global_box" v-for="(item,index) in dataList">
				<view class="left">
					<view class="content">
						<image class="mr30"
							:src="item.imgPath || item.executor. imgPath|| 'https://inews.gtimg.com/newsapp_bt/0/9680744091/1000'"
							mode="aspectFit|aspectFill|widthFix"></image>
						<view class="top">
							<view class="name">
								{{item.nickName || item.info.userName || item.executor.name}}
							</view>
							<view class="info">
								<text class="mr20">{{ item.duty || item.info.duty }}</text>
								<text>{{item.phone || item.executor.phone}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="right">
					<radio v-if="type=='acceptor' || type=='executor'" :checked="checkItemData.id==item.id"
						color="#fff" />
					<radio v-if="type=='transfer'" :checked="checkItemData.info.createUserId==item.info.createUserId"
						color="#fff" />
					<radio v-if="type=='setAssist'" :checked="item.flag" color="#fff" />
					<radio v-if="type=='removeAssist'"
						:checked="chekcItemList.find(item2=>item2.executor.id==(item.executor && item.executor.id))"
						color="#fff" />
				</view>
			</view>
		</scroll-view>
		<view class="footer" @click="submitItemHandle">
			确定
		</view>
	</view>
</template>

<script>
	import {
		request
	} from "../../request/index.js";

	export default {
		data() {
			return {
				query: {
					pageIndex: 1,
					pageSize: 10,
					projectId: uni.getStorageSync("projectId") || "16154516457903604960000255341083",
					nickName: "",
				},
				total: 0,
				pages: 0,
				type: "",
				dataList: [],
				checkItemData: {},
				chekcItemList: [],
				scrollStatus: false,
				handleQuery: {}
			};
		},
		onLoad(options) {
			console.log('options', options)
			this.type = options.type
			this.handleQuery = options.query && JSON.parse(options.query) || {}
			const title = options.title
			this.query.workflowId = options.workflowId
			this.query.stepId = options.stepId
			this.query.workOrderId = this.handleQuery.id
			uni.setNavigationBarTitle({
				title
			})

			if (this.type == "acceptor" || this.type == "executor") {
				//  执行人验收人获取
				this.getList1()
			} else if (this.type == "setAssist" || this.type == "transfer") {
				//  协助人获取
				this.getList2()
			} else if (this.type == "removeAssist") {
				//  获取关联协同人
				this.getRelevancyHandle()
			}
		},
		methods: {
			checkItemHandle(item) {
				if (this.type == "acceptor" || this.type == "executor" || this.type == 'transfer') {
					this.checkItemData = item
				} else if (this.type == "setAssist") {
					const index = this.chekcItemList.findIndex(item2 => item2.info.id == item.info.id)
					if (index != -1) this.chekcItemList.splice(index, 1)
					else this.chekcItemList.push(item)
				} else if (this.type == "removeAssist") {
					const index = this.chekcItemList.findIndex(item2 => item2.executor.id == item.executor.id)
					if (index != -1) this.chekcItemList.splice(index, 1)
					else this.chekcItemList.push(item)
				}
				this.$forceUpdate()
			},
			async getRelevancyHandle(status) {
				const res = await request({
					url: "api/oct/wemini/bus/workOrder/findExecutorPage",
					method: "POST",
					data: {
						executorType: 0,
						stepId: this.handleQuery.stepId,
						orderId: this.handleQuery.id,
						stepDefineFlag: this.handleQuery.stepDefineFlag,
						executionType: 2,
						pageIndex: 1,
						pageSize: 10
					}
				})
				if (res.status == 0) {
					if (status) return res
					else {
						this.dataList = res.payload.records
						this.totla = res.payload.total
						this.pages = res.payload.pages
					}
				} else {
					uni.showToast({
						title: "获取关联失败!",
						icon: "none",
						duration: 2000
					})
				}
			},
			async scrolltolowerHandle() {
				if (this.scrollStatus || (this.query.pageIndex == this.pages)) return

				this.scrollStatus = true
				this.query.pageIndex = this.query.pageIndex + 1

				let res = {}
				if (this.type == "acceptor" || this.type == "executor") {
					//  执行人验收人获取
					res = await this.getList1(true)
				} else if (this.type == "setAssist" || this.type == 'transfer') {
					//  协助人获取
					res = await this.getList2(true)
				} else if (this.type == "removeAssist") {
					res = await this.getRelevancyHandle(true)
				}

				this.dataList = [...this.dataList, ...res.payload.records]
				this.pages = res.payload.pages
				this.total = res.payload.total
				this.scrollStatus = false
			},
			confirmHandle() {
				this.query.pageIndex = 1
				this.chekcItemList = []
				if (this.type == "acceptor" || this.type == "executor") {
					//  执行人验收人获取
					this.getList1()
				} else if (this.type == "setAssist" || this.type == 'transfer') {
					//  协助人获取
					this.getList2()
				} else if (this.type == "removeAssist") {
					this.getRelevancyHandle()
				}
			},
			async submitItemHandle() {
				if (this.type == "acceptor" || this.type == "executor" || this.type == 'transfer') {
					//  执行人验收人获取
					if (JSON.stringify(this.checkItemData) == "{}") {
						return uni.showToast({
							title: '请选择人员',
							icon: 'none',
							duration: 1000,
							success: (res) => {

							},
						})
					}
				} else if (this.type == "removeAssist" || this.type == "setAssist") {
					//  协助人获取
					if (!this.chekcItemList.length) return uni.showToast({
						title: '请选择人员',
						icon: 'none',
						duration: 1000,
						success: (res) => {

						},
					})
				}

				//  拿到上级页面的数据对象直接赋值
				let routes = getCurrentPages();
				let curRoute = routes[routes.length - 2]
				let obj = curRoute.$vm
				if (this.type == "acceptor") {
					this.$set(obj.form, 'accepterId', this.checkItemData.id)
					this.$set(obj.form, 'accepterName', this.checkItemData.nickName)
					uni.navigateBack({
						delta: 1,
					})
				} else if (this.type == "executor") {
					this.$set(obj.form, 'executorId', this.checkItemData.id)
					this.$set(obj.form, 'executorName', this.checkItemData.nickName)
					uni.navigateBack({
						delta: 1,
					})
				} else if (this.type == "setAssist" || this.type == "removeAssist" || this.type == "transfer") {
					//  设置协同人与移除协同人是一样的参数只不过执行动作不一样而已 assistantIds
					//  工单处理

					let assistantIds = undefined
					let otherExecutorId = undefined
					if (this.type == "setAssist") assistantIds = this.chekcItemList.map(item => item.info.createUserId)
					else if (this.type == "removeAssist") assistantIds = this.chekcItemList.map(item => item.executor
						.id)
					else if (this.type == 'transfer') otherExecutorId = this.checkItemData.info.createUserId

					const res = await this.staffHandle({
						...this.handleQuery,
						assistantIds,
						otherExecutorId
					})
					if (res.status == 0) {
						uni.showToast({
							title: '处理成功!',
							icon: 'none',
							duration: 2000,
						})
						setTimeout(async () => {
							await curRoute.$vm.getWorkDetailHandle()
							curRoute.$vm.getButtonPermission()
						}, 1000)
						uni.navigateBack({
							delta: 1,
						})
					} else {
						uni.showToast({
							title: '处理失败!',
							icon: 'none',
							duration: 2000,
						})
					}

				}
			},
			async staffHandle(query) {
				const res = await request({
					url: "api/oct/wemini/bus/workOrder/handleWorkOrder",
					method: "POST",
					data: query
				})
				return res
			},
			clearInputHandle() {
				this.query.nickName = ""
				this.query.pageIndex = 1
				this.chekcItemList = []
				if (this.type == "acceptor" || this.type == "executor") {
					//  执行人验收人获取
					this.getList1()
				} else if (this.type == "setAssist") {
					//  协助人获取
					this.getList2()
				} else if (this.type == "removeAssist") {
					this.getRelevancyHandle()
				}
			},
			async getList1(status) {
				//  获取执行验收人员
				const res = await request({
					url: "api/oct/wemini/bus/workOrder/findProjectUserPage",
					method: "POST",
					data: this.query
				})
				if (res.status == 0) {
					if (status) return res
					else {
						this.dataList = res.payload.records
						this.total = res.payload.total
						this.pages = res.payload.pages
					}
				} else {
					uni.showToast({
						title: '获取失败!',
						icon: 'none',
						duration: 1000,
						success: (res) => {

						},
					})
				}
			},
			async getList2(status) {
				if (this.type == "setAssist") this.query.executionType = 2
				else if (this.type == "transfer") this.query.executionType = 1
				//  获取协同人分页
				const res = await request({
					url: "api/oct/wemini/bus/workOrder/findTransferUserPage",
					method: "POST",
					data: this.query
				})
				if (res.status == 0) {
					if (status) return res
					else {
						this.dataList = res.payload.records
						this.total = res.payload.total
						this.pages = res.payload.pages
					}
				} else {
					uni.showToast({
						title: '获取失败!',
						icon: 'none',
						duration: 1000,
						success: (res) => {

						},
					})
				}
			},
		},
	}
</script>

<style lang="less">
	.select-staff-main {
		background-color: #f2f6ff;
		padding: 10rpx 30rpx 88rpx;

		.search {
			background-color: #ffffff;
			height: 70rpx;
			border-radius: 39rpx;
			display: flex;
			padding-left: 20rpx;
			align-items: center;
			justify-content: space-between;

			.iconfont {
				color: #B1B6C5;
				line-height: 70rpx;
			}

			input {
				font-size: 25rpx;
				height: 70rpx;
				line-height: 70rpx;
				margin-left: 10rpx;
				width: 600rpx;
			}
		}

		.staff-list {
			height: calc(100vh - 200rpx);

			.item {
				height: 120rpx;
				margin: 30rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.left {
					.content {
						display: flex;
						align-items: center;

						.top {
							.info {
								font-size: 25rpx;
							}
						}

						image {
							width: 80rpx;
							height: 80rpx;
							border-radius: 50%;
						}
					}
				}
			}
		}

		.footer {
			height: 88rpx;
			width: 100vw;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #fff;
			background-color: @main_bg;
			position: fixed;
			left: 0;
			bottom: 0;
		}
	}
</style>